//
//  MyMbd.m
//  LimitFree
//
//  Created by llz on 14-12-15.
//  Copyright (c) 2014年 llz. All rights reserved.
//

#import "MyMbd.h"
#import "MBProgressHUD.h"
#import "Unit.h"

@interface  MyMbd ()
@property(nonatomic,assign)BOOL isStartAnimation;
@property(nonatomic,assign)NSInteger angle;
@end
@implementation MyMbd
//{
//    int angle;
//}
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.angle = 0;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenW/2-30, ScreenH/2-30, 60, 60)];
        
        self.imageView.center = CGPointMake(ScreenW/2, ScreenH/2);
        self.imageView.image = [UIImage imageNamed:@"椭圆-2"];
        [self addSubview:self.imageView];

    }
    return self;
}

+(void)addHudToView:(UIView *)view
{
    
    MyMbd *mbd =  [[self alloc]initWithFrame:view.frame];
    
    [mbd startAnimation];
    
    [view addSubview:mbd];
}

-(void)startAnimation
{
    self.hidden = NO;
    
    self.isStartAnimation = YES;
    
    [UIView animateWithDuration:0.05 animations:^{
        
        
    self.imageView.transform = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));

        
    } completion:^(BOOL finished) {
        [self endAnimation];

    }];
    
}

-(void)endAnimation
{
    if (self.isStartAnimation==NO) {
        return;
    }
    self.angle += 10;
    [self startAnimation];
}

+(void)hidenHudFromView:(UIView *)view
{
    
    if ([view subviews].count ==0) {
        return;
    }
    
    MyMbd *mbd = [view subviews].lastObject;
    
    mbd.isStartAnimation = NO;
    
    [mbd endAnimation];
    
    [mbd removeFromSuperview];
    
//    mbd.hidden = YES;

}


@end
